#!/bin/bash 
#
# Audio Picker Power and Mode Control
#

GPIO_AUD_PICKER_PWR=434
GPIO_AUD_PICKER_MODE_A0=444
GPIO_AUD_PICKER_MODE_A1=452

SYSFS_GPIO=/sys/class/gpio
SYSFS_AUD_PICKER_PWR=${SYSFS_GPIO}/PN.02
SYSFS_AUD_PICKER_MODE_A0=${SYSFS_GPIO}/PP.04
SYSFS_AUD_PICKER_MODE_A1=${SYSFS_GPIO}/PQ.04

CMD_HELP="help"
CMD_PWR="power"
CMD_MODE="mode"

function usage()
{
	echo -e "\nUsage: $0 [<cmd> [args]>]\n"
	echo -e "  The cmd can be one of:"
	echo -e "  help ------------------ Show this help"
	echo -e "  power ----------------- Audio Picker power operation"
	echo -e "    power <on|off> ------ Set power ON or OFF"
	echo -e "  mode ------------------ Audio Picker mode select operation"
	echo -e "    mode <0|1> <0|1> ---- Set mode A0 and A1 Value\n"
	echo -e "  For example:"
	echo -e "    $(basename $0) ------------- Get current power and mode status"
	echo -e "    $(basename $0) power on ---- Set power on"
	echo -e "    $(basename $0) mode 1 1 ---- Set mode A0=1 A1=1\n"
}

function gpio_init()
{
	if [[ ! -d ${SYSFS_AUD_PICKER_PWR} ]]; then
		echo ${GPIO_AUD_PICKER_PWR} > ${SYSFS_GPIO}/export
	fi
	if [[ ! -d ${SYSFS_AUD_PICKER_MODE_A0} ]]; then
		echo ${GPIO_AUD_PICKER_MODE_A0} > ${SYSFS_GPIO}/export
	fi
	if [[ ! -d ${SYSFS_AUD_PICKER_MODE_A1} ]]; then
		echo ${GPIO_AUD_PICKER_MODE_A1} > ${SYSFS_GPIO}/export
	fi
}

# GPIO High Active
function power_operate()
{
	if [[ $# -lt 1 ]]; then
		local value=$(cat ${SYSFS_AUD_PICKER_PWR}/value)

		if [[ ${value} -eq 1 ]]; then
			echo -e "[INFO] Audio Picker powered ON."
		elif [[ ${value} -eq 0 ]]; then
			echo -e "[INFO] Audio Picker powered OFF."
		else
			echo -e "[INFO] Audio Picker unknown power state."
		fi
	else
		case $1 in
			"on" | "ON")
				echo 1 > ${SYSFS_AUD_PICKER_PWR}/value
				echo -e "[INFO] Audio Picker power ON."
				;;
			"off" | "OFF")
				echo 0 > ${SYSFS_AUD_PICKER_PWR}/value
				echo -e "[INFO] Audio Picker power OFF."
				;;
			*)
				echo -e "[ERROR] Unknown power operation: $1, do nothing!"
				usage
				exit 1
				;;
		esac
	fi
}

# GPIO Low Active
function mode_operate()
{
	if [[ $# -eq 0 ]]; then
		local GPIO_A0_VALUE=$(cat ${SYSFS_AUD_PICKER_MODE_A0}/value)
		local GPIO_A1_VALUE=$(cat ${SYSFS_AUD_PICKER_MODE_A1}/value)
		local A0=0
		local A1=0

		if [[ ${GPIO_A0_VALUE} == 0 ]]; then
			local A0=1
		fi
		if [[ ${GPIO_A1_VALUE} == 0 ]]; then
			local A1=1
		fi

		echo -e "[INFO] Audio Picker current mode: A0=${A0} A1=${A1}."
	elif [[ $# -ge 2 ]]; then
		local A0=$1
		local A1=$2
		local GPIO_A0_VALUE=0
		local GPIO_A1_VALUE=0

		if [[ ${A0} == "0" ]]; then
			GPIO_A0_VALUE="1"
		fi
		if [[ ${A1} == "0" ]]; then
			GPIO_A1_VALUE="1"
		fi

		echo ${GPIO_A0_VALUE} > ${SYSFS_AUD_PICKER_MODE_A0}/value
		echo -e "[INFO] Audio Picker mode set A0=${A0}."

		echo ${GPIO_A1_VALUE} > ${SYSFS_AUD_PICKER_MODE_A1}/value
		echo -e "[INFO] Audio Picker mode set A1=${A1}."
	else
		echo -e "[ERROR] Unknown mode operation: $1, do nothing!"
		usage
		exit 1
	fi
}

##################################################################

if [[ $(id -u) -ne 0 ]]; then
	echo -e "[ERROR] Please run as root!"
	usage
	exit 1
fi

gpio_init

if [[ $# -eq 0 ]]; then
	power_operate
	mode_operate
else
	cmd=$1
	case ${cmd} in
		${CMD_PWR})
			shift
			power_operate $@
			;;
		${CMD_MODE})
			shift
			mode_operate $@
			;;
		${CMD_HELP})
			usage
			exit 1
			;;
		*)
			echo "[ERROR] Unsupport command: ${cmd}"
			usage
			exit 1
			;;
	esac
fi
